import AudioManager from "./AudioManager";
import PlayerDataSys from "./PlayerDataSys";
import OutsideGuideExperimentMgr from "./OutsideGuideExperimentMgr";
import PeripheralSystemDataMgr from "./PeripheralSystemDataMgr";
import EventMgr from "./EventMgr";
import GameEventType from "./GameEventType";
import frameworkManager from "./frameworkManager";
import EngineUtil from "./EngineUtil";
import BasePageCtrl from "./BasePageCtrl";
import PageMgr from "./PageMgr";
import PeripheralSystemMgr from "./PeripheralSystemMgr";
import UiManage from "./UiManage";
import FundWithDrawExperimentPage from "./FundWithDrawExperimentPage";

const { ccclass, property } = cc._decorator;

@ccclass
export default class FundWithDrawExperimentPageCtrl extends BasePageCtrl {
    v = cc._decorator;
    b = this.v.ccclass;
    C = this.v.menu;
    T = "#983B1A";
    M = ["旅游补贴不足，快闯关赚钱吧", "未到达第21个城市，快闯关赚钱吧", "未打卡30天，快闯关赚钱吧"];

    onLoad() {
        super.onLoad();
        this.onUILoad();
        this.addButtonListen();
        this.dealOppoReview();
        this.addEvent();
        this.scheduleOnce(() => {
            this.setVisibleArea();
        });
    }

    dealOppoReview() {
        // if (c.isHitOppoReviewBan()) {
        // this.ui.instructionsLabel.active = false;
        // this.ui.oppoInstructionsLabel.active = true;
        // } else {
        // this.ui.instructionsLabel.active = true;
        // this.ui.oppoInstructionsLabel.active = false;
        // }
    }

    addEvent() {
        EventMgr.listen(GameEventType.UPDATE_FUND, this.updatenNumberAnim, this);
    }

    removeEvent() {
        EventMgr.ignore(GameEventType.UPDATE_FUND, this.updatenNumberAnim, this);
    }

    onDestroy() {
        this.removeEvent();
    }

    start() {

    }

    onUILoad() {
        this.ui = this.node.addComponent(FundWithDrawExperimentPage);
    }

    addButtonListen() {
        UiManage.addButtonListen(this.ui.btn_close, this.clickClose, this);
        UiManage.addButtonListen(this.ui.btn_withdraw, this.clickWithDraw, this);
    }

    clickClose() {
        this.hide();
        PeripheralSystemDataMgr.getOutsideGuideStep() != OutsideGuideExperimentMgr.OutSideLevelGuideExperimentStep.FUND_GUIDE && AudioManager.getInstance().stopMusic("fund_withDraw_tip_experiment", false);
    }

    setVisibleArea() {

    }

    _init(e?) {
        if (e) {
            const t = e.gold_balance;
            const a = e.extract_info;
            this.gold_balance = t;
            this.extract_info = a;
            this.click_index = null;
            this.toast_type = null;
            this.updateFund();
            this.initGear();
            this.clickGender(this.ui.option1);
            this.checkGuide();
            this.ui.userID.getComponent(cc.Label).string = "Player ID:" + PlayerDataSys.user_id;
        }
    }

    updateFund() {
        this.ui.fundLabel.getComponent(cc.Label).string = EngineUtil.getCashBalance(this.gold_balance);
    }

    initGear() {
        const e = this.ui.optionBtn.children;
        for (let t = 0; t < e.length; t++) {
            const a = e[t];
            this.addButtonListenEvent(a, this.clickGender, this);
            const o = a.getChildByName("gearLabel");
            const i = this.extract_info[t].conditions[0].finish;
            o.color = EngineUtil.getColor(this.T);
            o.getComponent(cc.Label).string = EngineUtil.getCashBalance(i) + "元";
        }
    }

    getGearByIndex(e) {
        return this.extract_info[e - 1].conditions[0].finish;
    }

    addButtonListenEvent(e, t, a, o, i) {
        void 0 === o && (o = "click");
        void 0 === i && (i = 300);
        if (e) {
            const r = e.getComponent(cc.Button);
            if (!r) return;
            e.on(o, () => {
                AudioManager.getInstance().playMusic(i);
                if (t) {
                    t.bind(a)(e);
                    if (i) {
                        r.interactable = false;
                        setTimeout(() => {
                            if (cc.isValid(r)) {
                                r.interactable = true;
                            }
                        }, i);
                    }
                }
            }, a);
        }
    }

    clickGender(e) {
        const t = e.name;
        const a = t.replace(/[^0-9]/gi, "");
        if (this.click_index !== parseInt(a)) {
            this.click_index = parseInt(a);
            frameworkManager.log("data:======", this.click_index);
            this.showOneSelect(t);
            this.showCurrentPro();
        }
    }

    showOneSelect(e) {
        const t = this.ui.optionBtn.children;
        for (let a = 0; a < t.length; a++) {
            const o = t[a];
            const i = o.getChildByName("common");
            const r = o.getChildByName("select");
            const n = o.getChildByName("gearLabel");
            if (o.name == e) {
                i.active = false;
                r.active = true;
                n.color = EngineUtil.getColor("#FFE400");
                n.getComponent(cc.Label).string = EngineUtil.getCashBalance(this.getGearByIndex(this.click_index)) + "元";
            } else {
                i.active = true;
                r.active = false;
                n.color = EngineUtil.getColor(this.T);
                n.getComponent(cc.Label).string = EngineUtil.getCashBalance(this.getGearByIndex(parseInt(o.name.replace(/[^0-9]/gi, "")))) + "元";
            }
        }
    }

    showCurrentPro() {
        const e = this.getGearByIndex(this.click_index);
        const t = this.gold_balance;
        if (t < e) {
            this.ui.extractGold.getComponent(cc.Label).string = EngineUtil.getCashBalance(t);
            this.ui.extractPro.getComponent(cc.ProgressBar).progress = t / e;
            this.ui.extractPro_bg.getComponent(cc.ProgressBar).progress = t / e;
            this.updateExtractProLabel(EngineUtil.getCashBalance(t), EngineUtil.getCashBalance(e));
        } else {
            this.ui.extractGold.getComponent(cc.Label).string = EngineUtil.getCashBalance(e);
            this.ui.extractPro.getComponent(cc.ProgressBar).progress = 1;
            this.ui.extractPro_bg.getComponent(cc.ProgressBar).progress = 1;
            this.updateExtractProLabel(EngineUtil.getCashBalance(e), EngineUtil.getCashBalance(e));
        }
    }

    updateExtractProLabel(e, t) {
        this.ui.extractProLabel.getComponent(cc.Label).string = e + "/" + t;
    }

    clickWithDraw() {
        if (this.click_index) {
            let e = {};
            const t = this.getGearByIndex(this.click_index);
            const a = this.gold_balance;
            if (a >= t) {
                e = this.extract_info[this.click_index - 1].draw_id;
                AudioManager.getInstance().playMusic(301);
                PageMgr.show("ExtractTipsPage", e, () => {
                    this.hide();
                });
            } else {
                const o = EngineUtil.getCashBalance(t - a);
                AudioManager.getInstance().playMusic(302);
                UiManage.showToast(this.M[this.click_index - 1] + ", 还差" + o + "元");
            }
        } else {
            AudioManager.getInstance().playMusic(302);
            UiManage.showToast("请选择提现方式");
        }
    }
}